<template>
  <div>
    <!-- 提交开票信息 -->
    <el-dialog
      title="提交开票信息"
      :visible.sync="isShow"
      center
      :before-close="cancel"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <!-- <el-form-item
          label="发票编号"
          v-for="(item, index) in form.invoice_number_arr"
          :key="index"
          :prop="'invoice_number_arr.' + index"
          :rules="[
            { required: true, message: '请输入发票编号' },
            { type: 'number', message: '发票编号必须为数字值' },
          ]"
        >
          <el-input
            class="invInput"
            v-model.number="form.invoice_number_arr[index]"
            autocomplete="off"
            :maxlength="8"
          >
            <el-button
              slot="append"
              @click.prevent="removeInput(form.invoice_number_arr[index])"
              icon="el-icon-delete"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="form.invoice_number_arr.push('')"
          >添加发票编号</el-button
        >
        <Divider></Divider> -->
        <!-- <el-form-item label="发票数量" prop="number">
					<el-input v-model="form.number" autocomplete="off"></el-input>
        </el-form-item>-->
        <!-- <el-form-item label="开票日期" prop="ticket_at">
          <el-date-picker
            v-model="form.ticket_at"
            type="date"
            placeholder="开票日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item> -->
        <el-form-item label="开票金额" prop="invoicePrice">
          <el-input v-model.number="form.invoicePrice" autocomplete="off"></el-input>
        </el-form-item>

        <manualUpload
          :typeLimit="['.pdf', '.zip', '.rar', '.7z']"
          :numLimit="5"
          @change="upChange"
        >
        </manualUpload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          v-no-more-click:3000
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import manualUpload from "~/components/manualUpload";
export default {
  components: {
    manualUpload
  },
  data() {
    let moneyValidator = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("金额不能为空"));
      }
      if (!Number.isInteger(value)) {
        callback(new Error("金额只能是数字"));
      } else {
        callback();
      }
    };
    return {
      form: {
        // invoice_number_arr: [""],
        // number: "",
        // ticket_at: "",
        invoicePrice: "",
        file: null,
        ids: null
      },
      isShow: false,
      rules: {
        number: [
          {
            required: true,
            message: "请输入发票数量",
            trigger: "blur",
          },
        ],
        ticket_at: [
          {
            required: true,
            message: "请输入开票日期",
            trigger: "blur",
          },
        ],
        invoicePrice: [
          {
            validator: moneyValidator,
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    show(ids) {
      this.form.ids = ids
      this.isShow = true
    },
    upChange(data) {
      if (data.length) {
        this.form.file = data[0].raw;
      } else {
        this.form.file = null;
      }
    },
    cancel() {
      this.isShow = false;
      this.$refs["ruleForm"].resetFields();
    },
    //提交数据
    submitForm(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.fileListV.length <= 0) {
            this.$message({
              type: "warning",
              message: "请至少上传一个发票文件",
            });
          } else {
            //主要逻辑
            var form = new FormData();

            // form.append("ids", that.form.invoiceRecord);
            // form.append(
            //   "invoice_number",
            //   that.form.invoice_number_arr.join(",")
            // );
            // //form.append("number", that.form.number);
            // form.append("ticket_at", that.form.ticket_at);
            // form.append("money", that.form.money);
            
            for (let key in this.form) {
              form.append(key, this.form[key])
            }
            axios({
              method: "post",
              url: global.baseurl + "/api/invoice/update",
              data: form,
              headers: {
                "Content-Type": "multipart/form-data",
                Authorization: "Bearer " + this.$store.state.token,
              },
            }).then((res) => {
              if (res.data.status == "success") {
                this.$message({
                  type: "success",
                  message: "提交成功",
                });
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>